<?php
declare (strict_types=1);

namespace app\common\event\listeners\attach;

use app\common\logic\Attach;
use eduline\upload\Client as FileClient;

class Deleted
{
    public function handle(int $attachId)
    {
        try {

            // $attach = app(Attach::class)->where('id', $attachId)->where('stock', 'local')->findOrEmpty();
            $attach = app(Attach::class)->where('id', $attachId)->findOrEmpty();
            if (!$attach->isEmpty()) {
                // if ($attach->stock === 'local') {
                //     $filepath = $attach->filepath;
                //     if (file_exists($filepath)) {
                //         @unlink($filepath);
                //     }
                // }
                $fileClient = new  FileClient($attach->stock);
                $fileClient->delete($attach);

                $attach->force()->delete();
            }
        } catch (\Exception $exception) {
        }
    }
}